import svgIcons from '@/icons'
import errorLog from './error-log'

export default {
  install: (app, options) => {
    // app.config.globalProperties.$myMethod = key => {

    // }

    // app.provide('i18n', options)

    app.use(svgIcons)

    // TODO: Element Plus
    // https://element-plus.gitee.io/#/zh-CN/component/quickstart

    // 关闭当前TagsView
    app.config.globalProperties.$closeNowTagsView = function () {
      const thisfullPath = this.$route.fullPath
      this.$store.dispatch('tagsView/delView', this.$route).then(({ visitedViews }) => {
        let index = -1
        visitedViews.forEach(function (item, ind) {
          if (index != -1) {
            return
          }
          if (item.fullPath.split('/')[1] == thisfullPath.split('/')[1]) {
            index = ind
          }
        })
        const latestView = visitedViews.slice(index)[0]
        if (latestView) {
          this.$router.push(latestView.fullPath)
        } else {
          // now the default is to redirect to the home page if there is no tags-view,
          // you can adjust it according to your needs.
          if (this.$route.name === 'Dashboard') {
            // to reload home page
            this.$router.replace({ path: '/redirect' + this.$route.fullPath })
          } else {
            this.$router.push('/')
          }
        }
      })
    }
    app.config.globalProperties.$refreshNowTagsView = function () {
      this.$store.dispatch('tagsView/delCachedView', this.$route).then(() => {
        const { fullPath } = this.$route
        this.$nextTick(() => {
          this.$router.replace({
            path: '/redirect' + fullPath
          })
        })
      })
    }

    errorLog(app)
  }
}
